home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0018_Text File Positions.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-27  |  5.1 KB  |  201 lines

  1. Unit TextUnit;
  2.  
  3. Interface
  4.  
  5. {$B-,D-,E-,I-,L-,N-,X+}
  6.  
  7. Uses Dos;
  8.  
  9.   Function TextFilePos(Var andle:Text):LongInt;        { FilePos    }
  10.   Function TextFileSize(Var andle:Text):LongInt;       { FileSize   }
  11.   Procedure TextSeek(Var andle:Text;Pos:LongInt);      { Seek       }
  12.   Procedure TextBlockread(Var andle:Text; Var buf;     { Blockread  }
  13.                       count:Word; Var result:Word);
  14.   Procedure TextBlockWrite(Var andle:Text;  Var buf;   { BlockWrite }
  15.                         count:Word; Var result:Word);
  16.   Function BinEof(Var andle:Text):Boolean;             { eof ohne $1a   }
  17.   Function TextSeekRel(Var andle:Text; Count:LongInt):LongInt;
  18.                                                        { Relativer Seek }
  19.  
  20. Implementation
  21.  
  22. Const
  23.   ab_anfang=0;     { DosSeek }
  24.   ab_jetzig=1;
  25.   ab_ende=2;
  26.  
  27. Function DosSeek(Handle:Word; Pos:LongInt; wie:Byte):LongInt;
  28. Type dWord=Array[0..1] of Word;
  29. Var Regs:Registers;
  30.     erg:LongInt;
  31. begin
  32.   With Regs do begin
  33.     ah:=$42;
  34.     al:=wie;
  35.     bx:=Handle;                 { Dos-Handle }
  36.     cx:=dWord(Pos)[1];          { Hi-Word Position }
  37.     dx:=dWord(Pos)[0];          { Lo-Word Position }
  38.     MSDos(Regs);
  39.     if Flags and fCarry<>0 then begin
  40.       InOutRes:=ax;
  41.       erg:=0
  42.       end
  43.       else erg:=regs.ax+regs.dx*65536;
  44.   end;
  45.   DosSeek:=erg;
  46. end;
  47.  
  48. Function TextFilePos(Var andle:Text):LongInt;
  49. Var erg:LongInt;
  50. begin
  51.   erg:=DosSeek(Textrec(andle).Handle, 0, ab_jetzig)
  52.                    -TextRec(andle).Bufend
  53.                    +TextRec(andle).BufPos;
  54.    TextFilepos:=erg;
  55. end;
  56.  
  57. Function TextFileSize(Var andle:Text):LongInt;
  58. Var TempPtr, erg:LongInt;
  59. begin
  60.   Case TextRec(andle).Mode of
  61.     fmInput:with Textrec(andle) do begin
  62.               TempPtr:=DosSeek(Handle, 0, ab_jetzig);
  63.               erg:=DosSeek(Handle, 0, ab_ende);
  64.               DosSeek(Handle, TempPtr, ab_anfang);
  65.             end;
  66.     fmOutput:erg:=TextFilePos(andle);
  67.     else begin
  68.       erg:=0;
  69.       InOutRes:=1;
  70.     end;
  71.   end;
  72.   TextFileSize:=erg;
  73. end;
  74.  
  75. Procedure TextSeek(Var andle:Text; Pos:LongInt);
  76. Var aktpos:LongInt;
  77. begin
  78.   aktpos:=TextFilePos(andle);
  79.   if aktpos<>pos then With Textrec(andle) do begin
  80.     if Mode=fmOutput then flush(andle);
  81.     With Textrec(andle) do begin
  82.       if (aktpos+(bufend-bufpos)<Pos) or (aktpos>Pos) then
  83.        begin
  84.         bufpos:=0;
  85.         bufend:=0;
  86.         DosSeek(Textrec(andle).Handle, pos, ab_anfang);
  87.        end
  88.        else begin
  89.          inc(bufpos, pos-aktpos);
  90.        end;
  91.       end;
  92.   end;
  93. end;
  94.  
  95. Procedure TextBlockread(Var andle:Text; Var buf; count:Word; Var result:Word);
  96. Var R:Registers;
  97.     noch, ausbuf:Word;
  98.     posinTextbuf:Pointer;
  99. begin
  100.   if Textrec(andle).Mode<>fmInput then InOutRes:=1
  101.    else begin
  102.     With Textrec(andle) do
  103.      begin
  104.        noch:=bufend-bufpos;
  105.        if noch<>0 then
  106.          begin
  107.             if noch<count then ausbuf:=noch else ausbuf:=count;
  108.  
  109.            posinTextbuf:=Pointer(LongInt(bufptr)+bufpos);
  110.            move(posinTextbuf^, buf, ausbuf);
  111.            inc(bufpos, ausbuf);
  112.          end;
  113.      end;
  114.     if noch<count then With r do
  115.       begin
  116.         ds:=Seg(buf);
  117.         dx:=Ofs(Buf)+noch;
  118.         ah:=$3f;
  119.         bx:=Textrec(andle).Handle;
  120.         cx:=count-noch;
  121.         MsDos(R);
  122.         if Flags and fCarry<>0
  123.           then InOutRes:=ax
  124.           else result:=ax+noch;
  125.       end
  126.       else result:=count;
  127.    end;
  128. end;
  129.  
  130. Procedure TextBlockWrite(Var andle:Text; Var buf; count:Word;Var result:Word);
  131. Var r:Registers;
  132.     posinTextbuf:Pointer;
  133. begin
  134.   if Textrec(andle).Mode<>fmOutput then InOutRes:=1
  135.    else begin
  136.      With Textrec(andle) do begin
  137.        if (bufsize-bufpos)>count then
  138.         begin
  139.           posinTextbuf:=Pointer(LongInt(bufptr)+bufpos);
  140.           move(buf, posinTextbuf^, count);
  141.           inc(bufpos, count);
  142.         end
  143.         else begin
  144.           flush(andle);
  145.           With r do begin
  146.             ah:=$40;
  147.             cx:=count;
  148.             ds:=seg(buf);
  149.             dx:=ofs(buf);
  150.             bx:=Handle;
  151.             MsDos(r);
  152.             if Flags and fCarry<>0 then InOutRes:=ax
  153.                                    else Result:=ax;
  154.           end;
  155.         end;
  156.        end;
  157.    end;
  158. end;
  159.  
  160. Function TextSeekRel(Var andle:Text; count:LongInt):LongInt;
  161. Var ziel, erg:LongInt;
  162. begin
  163.   With Textrec(andle) do begin
  164.     if Mode=fmOutput then begin InOutRes:=1; Exit; end;
  165.     if (count<0) then
  166.       begin
  167.         ziel:=TextFilePos(andle)+count;
  168.         if ziel<0 then ziel:=0;
  169.         TextSeek(andle, ziel);
  170.         erg:=ziel;
  171.       end
  172.     else if ((bufend-bufpos)<Count) then
  173.       begin
  174.         ziel:=count-(bufend-bufpos);
  175.         if ziel<0 then ziel:=0;
  176.         erg:=DosSeek(Textrec(andle).Handle, ziel, ab_jetzig);
  177.         bufpos:=0; bufend:=0;
  178.       end
  179.       else begin
  180.         inc(bufpos, count);
  181.         erg:=maxLongInt;
  182.       end;
  183.   TextSeekRel:=erg;
  184.   end;
  185. end;
  186.  
  187.  
  188. Function BinEof(Var andle:Text):Boolean;
  189. Var e:Boolean;
  190. begin
  191.   e:=eof(andle);
  192. {$R-}
  193.   With Textrec(andle) do
  194.     BinEof:=e and (bufptr^[bufpos]<>#$1a);
  195. {$R+}
  196. end;
  197.  
  198.  
  199. end.
  200.  
  201.